package gamesrv.clientsim;



import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import gamesrv.api.*;

import org.osgi.framework.ServiceReference;

public class UserThread extends Thread  {
	String userName;
	String type;
	Pattern ticktacktoePattern = Pattern.compile("(.+)\\|(.+)\\|(.+)[\\r\\n\\-]*(.+)\\|(.+)\\|(.+)[\\r\\n\\-]*(.+)\\|(.+)\\|(.+)[\\r\\n\\-]*");
	
	public static String TicTacToe = "Tic-tac-toe";
	
	
	void sleep()
	{
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {					// 
			e.printStackTrace();
		}
	}
	
	
	
	
	public UserThread(String userName,String type)
	{
		this.userName = userName;
		this.type = type;
	}
	
	
	
	public void run()
	{
		
		ServiceReference<GameServer> sr = Activator.getContext().getServiceReference(GameServer.class);
		if (sr == null )
			return;
		
		GameServer gs= Activator.getContext().getService(sr);
		
		while(true)
		{
			if (!gs.getGameTypes().contains(type))
			{
				sleep();
				continue;
			}
			
			GameHandle game = gs.startGame(type, userName);					
			
			System.out.println("User " + userName + " started game of " + type);
			
			while(true)
			{
			
				while(game.getStatus() == GameStatus.Waiting)				
					sleep();
				
				if (game.getStatus()== GameStatus.Finished)
					break;
				
				if (game.getStatus()== GameStatus.ReadyForMove)
				{				
					String[] move = getMove(type,game);
					sleep();
					sleep();
					sleep();
					try
					{
						System.out.println("User " + userName + " made move: " + move);
						game.move(move);
					}
					catch (IllegalMoveException e)
					{
						System.out.println("User " + userName + " encoutered IllegalMoveException");
					} catch (GameNotReadyException e) {
						System.out.println("User " + userName + " encoutered GameNotReadyException exception");			
					} catch (NotMyMoveException e) {
						System.out.println("User " + userName + " encoutered NotMyMoveException exception");
					}
				}
			}
		}			
	}




	private String[] getMove(String type, GameHandle game) {
		if (type == TicTacToe)
			return getTickTackToeMove(game);
		return null;
	}


	

	private String[] getTickTackToeMove(GameHandle game) {
		ArrayList<int[]> freeSlots = new ArrayList<int[]> ();
		String[][] status;
		
		/*String[][] status =  new String[3][3];
		
		String situation = game.getSituation();
		Matcher match = ticktacktoePattern.matcher(situation);
		if ( !match.find())
			return null;
						 
		for (int i=0; i < 3; i++)
			for (int j=0; j< 3; j++)
			{
				String f = match.group(i*3 + j +1);
				status[i][j] = f;						
			}
			*/
		status = game.getSituation();
		for (int i=0; i < 3; i++)
			for (int j=0; j< 3; j++)
			{			
				if (status[i][j].equals(" "))
				{
					int [] slot = new int[2];
					slot[0] = i;
					slot[1] = j;
					freeSlots.add(slot);
				}				
			}
		
		int[] slot =freeSlots.get((int)( Math.random() * freeSlots.size()));
		
		String[] move ={"" + (slot[0]) ,""+ slot[1]}; 
		return move;
		
			
		
		
	}

}
